package gamagora.game
{
	import Box2D.Dynamics.b2Body;
	
	import gamagora.box2d.OSPhysicCircle;
	import gamagora.box2d.OSPhysicData;
	import gamagora.box2d.OSPhysicSprite;
	import gamagora.box2d.OSPhysicWorld;
	import gamagora.game.map.OSWorldMap;
	
	import org.flixel.FlxEmitter;
	import org.flixel.FlxG;
	import org.flixel.FlxGroup;
	import org.flixel.FlxParticle;
	import org.flixel.FlxSprite;
	
	public class OSWaterGenerator extends FlxSprite
	{
		private var _emitter:FlxEmitter;
		private var _maxY:Number;

		public function OSWaterGenerator(x:Number, y:Number, nbParticles:uint, maxY:Number, direction:String)
		{
			super(x, y);
			_maxY = maxY;
			
			_emitter = new FlxEmitter(x, y, nbParticles);
			_emitter.setYSpeed(-50, 50);
			_emitter.gravity = 200;
			
			if(direction == "left")
			{
				_emitter.setXSpeed(-50, 0);
				
				
			}
			else
			{
				_emitter.setXSpeed(0, 50);
			}
			
			var particle:FlxParticle; 
			for (var i:int = 0; i < _emitter.maxSize / 2; i++) 
			{
				particle = new FlxParticle();
				particle.makeGraphic(2, 2, 0xFF3399FF);
				particle.visible = false;
				_emitter.add(particle);
				particle = new FlxParticle();
				particle.makeGraphic(1, 1, 0xFF3399FF);
				particle.visible = false;
				_emitter.add(particle);
			}
			
			_emitter.start(false, 5, .02);
		}
		
		public override function update():void
		{
			for(var i:uint=0;i<_emitter.members.length;i++)
			{
				var p:FlxParticle = _emitter.members[i] as FlxParticle;
				if(p.y >= _maxY)
				{
					p.velocity.x = Math.random() * 100 - 50;
					p.velocity.y = -50;
				}
			}
			_emitter.update();
		}
		
		public override function draw():void
		{
			_emitter.draw();
		}
		
		public override function destroy():void
		{
			super.destroy();
			_emitter.destroy();
		}
	}
}
